package com.uci.photogam.facebook;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.uci.photogam.R;
import com.uci.photogam.V;

/**
 * Shows the list of albums in your facebook account
 * @author Alexis
 *
 */
public class FacebookAlbumList extends Activity implements OnItemClickListener{
	private Handler mHandler;
	protected static ArrayList<JSONObject> validJSONAlbums;
	protected ListView list;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mHandler = new Handler();
		setContentView(R.layout.fb_albums);

		validJSONAlbums = new ArrayList<JSONObject>();
		//get the facebook json response that was packed in to this activity.
		Bundle extras = getIntent().getExtras();
		String apiResponse = extras.getString("API_RESPONSE");
		try {
			JSONArray jsonArray = new JSONObject(apiResponse).getJSONArray("data");
			for(int i = 0; i < jsonArray.length(); i++)
			{
				try{
					JSONObject object = jsonArray.getJSONObject(i);
					object.get("count");
					validJSONAlbums.add(object);
				}catch (JSONException e1)
				{
					//if there's no images, don't display album
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		list = (ListView)this.findViewById(R.id.fb_album_list);
		list.setOnItemClickListener(this);
		list.setAdapter(new AlbumAdapter(this));
	}

	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		try {
			long id = validJSONAlbums.get(position).getLong("id");
			//System.out.println(id + "," + position);
			Utility.mAsyncRunner.request(id + "/photos", new GetPhotosRequestListener());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	//This gets called and receives the json file for images
	class GetPhotosRequestListener extends BaseRequestListener
	{
		public void onComplete(String response, Object state) {
			Intent intent = new Intent(getApplicationContext(), FacebookAlbumGallery.class);
			intent.putExtra("API_RESPONSE", response);
			//Utility.model = null;
			startActivityForResult(intent, V.REQ_CODE_PICK_PICTURE_FB);
		}	
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if(resultCode == RESULT_OK && requestCode == V.REQ_CODE_PICK_PICTURE_FB)
		{
			FacebookAlbumList.this.setResult(RESULT_OK, data);
			FacebookAlbumList.this.finish();
		}
		
	}

	/**
	 * Adapter which loads the album names into the list
	 * @author Alexis
	 *
	 */
	private class AlbumAdapter extends BaseAdapter{
		private LayoutInflater mInflater;
		//private Context context;

		public AlbumAdapter(FacebookAlbumList gallery)
		{
			//this.context = context;
			if(Utility.model == null)
			{
				Utility.model = new FacebookGalleryGetImages();
			}
			Utility.model.setListener(this);
			mInflater = LayoutInflater.from(list.getContext());
		}

		public int getCount() {
			return validJSONAlbums.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		/**
		 * sets the album name for each item on the list by parsing the json response
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
			JSONObject jsonObject = null;
			//ImageView image = new ImageView(context);
			//
			jsonObject = validJSONAlbums.get(position);
			
			//if the particular album list item does not exist, create it
			View hView = convertView;
			if (convertView == null) {
				hView = mInflater.inflate(R.layout.fb_album_item, null);
				ViewHolder holder = new ViewHolder();
				holder.name = (TextView)hView.findViewById(R.id.fb_album_name);
				hView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) hView.getTag();
			try {
				holder.name.setText(jsonObject.getString("name") + " (" + jsonObject.getString("count") + ")");
			} catch (JSONException e) {
				holder.name.setText("");
			}

			return hView;
		}

	}

	/**
	 * Holds the name of the album in the album list
	 * @author Alexis
	 *
	 */
	class ViewHolder {
		TextView name;
	}
}
